package com.onlineMIS.action.headQ.ipad;



import java.text.ParseException;
import java.util.Collections;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.onlineMIS.ORM.DAO.Response;
import com.onlineMIS.ORM.DAO.headQ.inventory.InventoryService;
import com.onlineMIS.ORM.DAO.headQ.inventoryFlow.HeadQInventoryStoreDAOImpl;
import com.onlineMIS.ORM.DAO.headQ.ipad.IpadService;
import com.onlineMIS.ORM.entity.headQ.SQLServer.ClientsMS;
import com.onlineMIS.ORM.entity.headQ.inventory.InventoryOrder;
import com.onlineMIS.ORM.entity.headQ.inventory.InventoryOrderProduct;
import com.onlineMIS.ORM.entity.headQ.inventoryFlow.HeadQInventoryStore;
import com.onlineMIS.ORM.entity.headQ.inventoryFlow.HeadqInventoryFlowOrder;
import com.onlineMIS.ORM.entity.headQ.inventoryFlow.HeadqInventoryFlowOrderProduct;
import com.onlineMIS.ORM.entity.headQ.user.UserInfor;
import com.onlineMIS.action.headQ.inventory.InventoryOrderJSONAction;
import com.onlineMIS.common.Common_util;
import com.onlineMIS.common.SessionInfo;
import com.onlineMIS.common.loggerLocal;
import com.onlineMIS.sorter.InventoryOrderProductSort;
import com.opensymphony.xwork2.ActionContext;

@Controller
public class IpadJSPAction extends IpadAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = -5368542996850921741L;
	
	@Autowired
	protected InventoryService inventoryService;

	@Autowired
	protected HeadQInventoryStoreDAOImpl headQInventoryStoreDAOImpl;

	@SuppressWarnings("unchecked")
	/**
	 * for the admin to edit the user's information
	 * @return
	 */
	public String goToEditCust(){

	    loggerLocal.info("IpadJSPAction - goToEditCust");
		
		return "EditCust";
	}
	
	public String logoff(){
		loggerLocal.info("IpadJSPAction - logoff");
		
		ActionContext.getContext().getSession().clear();
		
		return "ipadIndex";
	}
	
	/**
	 * 总部开始盘点默认的客户
	 * @return
	 */
	public String startOrderByDefaultClient() {
		//int clientId = 2044;  2025.6.30 停用，这里修改为库存单
		UserInfor loginUser = (UserInfor)ActionContext.getContext().getSession().get(Common_util.LOGIN_USER);
		String clientS = "公司盘点";
		ActionContext.getContext().getSession().put(IpadConf.HQ_SESSION_INFO_CUSTNAME, clientS);
		ActionContext.getContext().getSession().put(IpadConf.HQ_SESSION_INFO_CLIENT_ID, loginUser.getUser_id());//总部员工信息
		ActionContext.getContext().getSession().put(IpadConf.HQ_SESSION_INFO_ORDER_ID, null);
		ActionContext.getContext().getSession().put(IpadConf.HQ_SESSION_INFO_ORDER_MODE, IpadService.ORDER_MODE_HAND);
		
		return "picSearch";

	}
	
	public String viewCurrentOrder4Pandian(){
		loggerLocal.info("IpadJSPAction - viewCurrentOrder4Pandian");
		
		Object orderIdObj =  ActionContext.getContext().getSession().get(IpadConf.HQ_SESSION_INFO_ORDER_ID);
		
		Response response = ipadService.getOrder4Pandian(orderIdObj);
		
		if (response.isSuccess()){
			
			HeadqInventoryFlowOrder order = (HeadqInventoryFlowOrder)response.getReturnValue();
			HeadQInventoryStore store = headQInventoryStoreDAOImpl.get(order.getStoreId(), true);
			
			int orderId = order.getId();
			int clientId = order.getCreator().getUser_id();
			String clientName =  order.getCreator().getUser_name();
			String storeName = "";
			if (store != null) {
				storeName = store.getName();
			}
			
			ActionContext.getContext().getSession().put(IpadConf.HQ_SESSION_INFO_ORDER_ID, orderId);
			ActionContext.getContext().getSession().put(IpadConf.HQ_SESSION_INFO_CLIENT_ID, clientId);
			ActionContext.getContext().getSession().put(IpadConf.HQ_SESSION_INFO_ORDER_STORE_ID, store.getId());
			ActionContext.getContext().getSession().put(IpadConf.HQ_SESSION_INFO_CUSTNAME, orderId+storeName+ ":"+clientName);
			
			List<HeadqInventoryFlowOrderProduct> orderProducts = order.getProductList();

			String productCode = formBean.getProductCode().toUpperCase();
			if (!productCode.equalsIgnoreCase("")){
				for (int i=orderProducts.size()-1; i>=0 ; i--){
					HeadqInventoryFlowOrderProduct orderProduct = orderProducts.get(i);
					String theProductCode = orderProduct.getProductBarcode().getProduct().getProductCode().toUpperCase();
					if (theProductCode.indexOf(productCode) == -1){
						orderProducts.remove(i);
					}
				}
			}
			
			List<InventoryOrderProductVO> orderProductVOs = InventoryOrderProductVO.parseHeadq(orderProducts);
			uiBean.setOrderProducts(orderProductVOs);
			uiBean.setTotalQ(order.getTotalQuantity());
			uiBean.setTotalW((int)order.getTotalWholePrice());
		}
		
		return "viewCurrentOrder";
	}
	
	public String viewCurrentOrder2(){
		loggerLocal.info("IpadJSPAction - viewCurrentOrder");
		
		Object orderIdObj =  ActionContext.getContext().getSession().get(IpadConf.HQ_SESSION_INFO_ORDER_ID);
		
		Response response = ipadService.getOrder(orderIdObj);
		
		if (response.isSuccess()){

			HeadqInventoryFlowOrder order = (HeadqInventoryFlowOrder)response.getReturnValue();
			
			int orderId = order.getId();
			int clientId = order.getStoreId();
			//String clientName = order.getClient_name();
			
			ActionContext.getContext().getSession().put(IpadConf.HQ_SESSION_INFO_ORDER_ID, orderId);
			ActionContext.getContext().getSession().put(IpadConf.HQ_SESSION_INFO_CLIENT_ID, clientId);
			ActionContext.getContext().getSession().put(IpadConf.HQ_SESSION_INFO_CUSTNAME, "测试库房");
			
			List<HeadqInventoryFlowOrderProduct> orderProducts = order.getProductList();
						
			//Collections.sort(orderProducts, new InventoryOrderProductSort());
			
			String productCode = formBean.getProductCode().toUpperCase();
			if (!productCode.equalsIgnoreCase("")){
				for (int i=orderProducts.size()-1; i>=0 ; i--){
					HeadqInventoryFlowOrderProduct orderProduct = orderProducts.get(i);
					String theProductCode = orderProduct.getProductBarcode().getProduct().getProductCode().toUpperCase();
					if (theProductCode.indexOf(productCode) == -1){
						orderProducts.remove(i);
					}
				}
			}
						
			List<InventoryOrderProductVO> orderProductVOs = InventoryOrderProductVO.parseHeadq(orderProducts);
			uiBean.setOrderProducts(orderProductVOs);
			uiBean.setTotalQ(order.getTotalQuantity());
			uiBean.setTotalW((int)order.getTotalWholePrice());
		}
		
		return "viewCurrentOrder";
	}
	
	/**
	 * 盘点系统-查询草稿订单信息
	 * @return
	 */
	public String listDraftOrder4Pandian(){
		loggerLocal.info("IpadJSPAction - listDraftOrder4Pandian");
		UserInfor loginUser = (UserInfor)ActionContext.getContext().getSession().get(Common_util.LOGIN_USER);
		
		Response response = ipadService.getDraftOrders4Pandian(loginUser);
		
		if (response.isSuccess()){			
			List<HeadqInventoryFlowOrder> orders = (List<HeadqInventoryFlowOrder>)response.getReturnValue();
			List<InventoryOrderVO> orderVOs = InventoryOrderVO.parseHeadq(orders);
			
			uiBean.setOrders(orderVOs);

		}
		
		return "listDraftOrder";
	}
	
	public String viewCurrentOrder(){
		loggerLocal.info("IpadJSPAction - viewCurrentOrder");
		
		Object orderIdObj =  ActionContext.getContext().getSession().get(IpadConf.HQ_SESSION_INFO_ORDER_ID);
		
		Response response = ipadService.getOrder(orderIdObj);
		
		if (response.isSuccess()){
			InventoryOrder order = (InventoryOrder)response.getReturnValue();
			
			int orderId = order.getOrder_ID();
			int clientId = order.getClient_id();
			String clientName = order.getClient_name();
			
			ActionContext.getContext().getSession().put(IpadConf.HQ_SESSION_INFO_ORDER_ID, orderId);
			ActionContext.getContext().getSession().put(IpadConf.HQ_SESSION_INFO_CLIENT_ID, clientId);
			ActionContext.getContext().getSession().put(IpadConf.HQ_SESSION_INFO_CUSTNAME, clientName);
			
			List<InventoryOrderProduct> orderProducts = order.getProduct_List();
			
			Collections.sort(orderProducts, new InventoryOrderProductSort());
			
			String productCode = formBean.getProductCode().toUpperCase();
			if (!productCode.equalsIgnoreCase("")){
				for (int i=orderProducts.size()-1; i>=0 ; i--){
					InventoryOrderProduct orderProduct = orderProducts.get(i);
					String theProductCode = orderProduct.getProductBarcode().getProduct().getProductCode().toUpperCase();
					if (theProductCode.indexOf(productCode) == -1){
						orderProducts.remove(i);
					}
				}
			}
			
			List<InventoryOrderProductVO> orderProductVOs = InventoryOrderProductVO.parse(orderProducts);
			uiBean.setOrderProducts(orderProductVOs);
			uiBean.setTotalQ(order.getTotalQuantity());
			uiBean.setTotalW((int)order.getTotalWholePrice());
		}
		
		return "viewCurrentOrder";
	}
	
	public String listDraftOrder(){
		loggerLocal.info("IpadJSPAction - listDraftOrder");
		UserInfor loginUser = (UserInfor)ActionContext.getContext().getSession().get(Common_util.LOGIN_USER);
		
		Response response = ipadService.getDraftOrders(loginUser);
		
		if (response.isSuccess()){
			List<InventoryOrder> orders = (List<InventoryOrder>)response.getReturnValue();
			
			List<InventoryOrderVO> orderVOs = InventoryOrderVO.parse(orders);
			uiBean.setOrders(orderVOs);

		}
		
		return "listDraftOrder";
	}
	
	/**
	 * pda登录
	 * @return
	 */
	public String goToEditCustPDA(){
	    loggerLocal.info("IpadJSPAction - goToEditCustPDA");
		
		return "EditCustPDA";
	}
	
	public String logoffPDA(){
		loggerLocal.info("IpadJSPAction - logoffPDA");
		
		ActionContext.getContext().getSession().clear();
		
		return "ipadIndexPDA";
	}
	
	public String listDraftOrderPDA(){
		loggerLocal.info("IpadJSPAction - listDraftOrderPDA");
		
		UserInfor loginUser = (UserInfor)ActionContext.getContext().getSession().get(Common_util.LOGIN_USER);
		
		Response response = ipadService.getDraftOrders(loginUser);
		
		if (response.isSuccess()){
			List<InventoryOrder> orders = (List<InventoryOrder>)response.getReturnValue();
			
			List<InventoryOrderVO> orderVOs = InventoryOrderVO.parse(orders);
			uiBean.setOrders(orderVOs);

		}
		
		return "listDraftOrderPDA";
	}
	
	public String viewCurrentOrderPDA(){
		
		loggerLocal.info("IpadJSPAction - viewCurrentOrderPDA");
		
		Object orderIdObj =  ActionContext.getContext().getSession().get(IpadConf.HQ_SESSION_INFO_ORDER_ID);
		
		Response response = ipadService.getOrder(orderIdObj);
		
		if (response.isSuccess()){
			InventoryOrder order = (InventoryOrder)response.getReturnValue();
			
			int orderId = order.getOrder_ID();
			int clientId = order.getClient_id();
			String clientName = order.getClient_name();
			
			ActionContext.getContext().getSession().put(IpadConf.HQ_SESSION_INFO_ORDER_ID, orderId);
			ActionContext.getContext().getSession().put(IpadConf.HQ_SESSION_INFO_CLIENT_ID, clientId);
			ActionContext.getContext().getSession().put(IpadConf.HQ_SESSION_INFO_CUSTNAME, clientName);
			
			List<InventoryOrderProduct> orderProducts = order.getProduct_List();
			
			Collections.sort(orderProducts, new InventoryOrderProductSort());
			
			String productCode = formBean.getProductCode().toUpperCase();
			if (!productCode.equalsIgnoreCase("")){
				for (int i=orderProducts.size()-1; i>=0 ; i--){
					InventoryOrderProduct orderProduct = orderProducts.get(i);
					String theProductCode = orderProduct.getProductBarcode().getProduct().getProductCode().toUpperCase();
					if (theProductCode.indexOf(productCode) == -1){
						orderProducts.remove(i);
					}
				}
			}
			
			List<InventoryOrderProductVO> orderProductVOs = InventoryOrderProductVO.parse(orderProducts);
			uiBean.setOrderProducts(orderProductVOs);
			uiBean.setTotalQ(order.getTotalQuantity());
			uiBean.setTotalW((int)order.getTotalWholePrice());
		}
		
		return "viewCurrentOrderPDA";
	}
	

	
	/**
	 * 搜索配货单据
	 * @return
	 */
	public String goSearch() {
		loggerLocal.info("IpadJSPAction - goSearch");
		
		SessionInfo sessionInfo = (SessionInfo)ActionContext.getContext().getSession().get(Common_util.LOGIN_USER_MOBILE);
		inventoryService.initIpadSearch(formBean,uiBean,sessionInfo);
		return "searchOrder";				
	}
	
	/**
	 * 配货详情-退回主页
	 * @return
	 * @throws ParseException 
	 */
	public String goBack() throws ParseException {
		loggerLocal.info("IpadJSPAction - goBack");
		SessionInfo sessionInfo = (SessionInfo)ActionContext.getContext().getSession().get(Common_util.LOGIN_USER_MOBILE);
		
		Integer custRange = (Integer)ActionContext.getContext().getSession().get(Common_util.IPAD_DISTRIBUTION_SELECT_RANGE);
		boolean isNoEquippen = inventoryService.goBackHome(formBean,uiBean,sessionInfo, custRange);
		if (isNoEquippen) {
			return "noEquipped";
		}else {
			return "alread";
		}			
	}
	
	/**
	 * 进入待配页面
	 * @return
	 * @throws ParseException 
	 */
	public String goNoEquipped() {
		loggerLocal.info("IpadJSPAction - goNoEquipped");
		SessionInfo sessionInfo = (SessionInfo)ActionContext.getContext().getSession().get(Common_util.LOGIN_USER_MOBILE);
		
		Integer custRange = (Integer)ActionContext.getContext().getSession().get(Common_util.IPAD_DISTRIBUTION_SELECT_RANGE);
		
		inventoryService.initIpadNoEquipped(formBean,uiBean,sessionInfo,custRange);
		return "noEquipped";				
	}
	
	/**
	 * 进入配货完成页面
	 * @return
	 * @throws ParseException 
	 */
	public String goAlread() {
		loggerLocal.info("IpadJSPAction - goAlread");
		SessionInfo sessionInfo = (SessionInfo)ActionContext.getContext().getSession().get(Common_util.LOGIN_USER_MOBILE);
		
		Integer custRange = (Integer)ActionContext.getContext().getSession().get(Common_util.IPAD_DISTRIBUTION_SELECT_RANGE);
		inventoryService.initIpadAlread(formBean,uiBean,sessionInfo, custRange);
		return "alread";				
	}

	
	/**
	 * 单据详情
	 * enterSearchDetails
	 * @return
	 */
	public String loadPeihuoOrder() {
		loggerLocal.info("IpadJSPAction - loadPeihuoOrder");
		SessionInfo sessionInfo = (SessionInfo)ActionContext.getContext().getSession().get(Common_util.LOGIN_USER_MOBILE);
		
		Response response = inventoryService.loadPeihuoOrder(formBean,sessionInfo);
		
		switch (response.getAction()) {
		   case 1:
			return "editPeihuoOrder";
		   case 2 : 
			return "viewPeihuoOrderDetails";

		   default:
			return "viewPeihuoOrderDetails";
		}
	}
	
	/**
	 * 总部客户信息组件
	 * @return
	 */
	public String enterHeadqCust() {
		
		return "enterHeadqCust";
		
	}

}
